import { action, toJS } from 'mobx';
import BaseListStore from './common/BaseListStore';
import * as API_SEARCHREQ from '../apis/searchReqApi';
import { WeaFormItem, WeaSearchGroup } from 'ecCom';
import { WeaSwitch } from 'comsMobx';

/**
 * 公文流程查询 数据对象
 */
export default class SearchReqStore extends BaseListStore {
  constructor(locationQuery = {}) {
    super();
    this.setTopTitle(this.getLabel2('384729', '公文流程查询'));
    this.API_TABCOUNT = '';
    this.searchStore.setSearchType(2);
    if (Object.prototype.hasOwnProperty.call(locationQuery, 'isNew')
      && (locationQuery.isNew == true || locationQuery.isNew == 'true')) {
      this.tabComponentParams = {
        getFormUrl: '/api/odoc/odocTodoList/getCustomCondition',
        searchParams: locationQuery,
        baseSearchName: 'requestname',
      };
      this.tabComponentParams = {
        getDataList: params => this.doSearch(params),
        searchType: ['base', 'advanced'],
        getFormUrl: '/api/odoc/odocTodoList/getCustomCondition',
        searchParams: locationQuery,
        baseSearchName: 'requestname',
      };
      if (Object.prototype.hasOwnProperty.call(locationQuery, 'officalType')) {
        locationQuery.officalType == '1' && this.setTopTitle(this.getLabel2('', '发文查询'));
        locationQuery.officalType == '2' && this.setTopTitle(this.getLabel2('', '收文查询'));
        locationQuery.officalType == '3' && this.setTopTitle(this.getLabel2('', '签报查询'));
      }
      this.showNewSearch = true;
    } else {
      this.showNewSearch = false;
    }
    this.locationquery = locationQuery;
  }


  getSearchsBaseValue() {
    return this.searchStore.form.getFormParams().requestname;
  }

  setSearchsBaseValue(v) {
    this.searchStore.form.updateFields({
      requestname: {
        value: v,
      },
    });
    this.searchStore.isSearched();
  }

  @action.bound
  doSearch(params = {}) {
    let param_todoList = this.searchStore.getFormParams();
    param_todoList = { ...param_todoList, ...this.searchStore.otherSearchParams, ...this.locationquery, ...params };
    API_SEARCHREQ.getSearchReqList(param_todoList).then((data) => {
      this.searchStore.tableStore.getDatas(data.generateParams.sessionkey);
    });
    this.searchStore.setShowSearchAd(false);
    return this;
  }

  buildSearchCondition(params = {}) {
    API_SEARCHREQ.getCondition(params).then((data) => {
      this.searchStore.setSearchCondition(data);
      const conditioninfo = data.condition;
      this.searchStore.form.initFormFields(conditioninfo);
      this.doSearch();
    });
  }

  resetForm() {
    this.searchStore.resetSearchForm();
    this.setSearchsBaseValue('');
  }

  getColumns = (columns) => {
    let newColumns = columns.map(column =>
      // if(column.dataIndex == "odoctype"){
      //   return {
      //     ...column, render: (text, record, index) => {
      //       return (<p  class="wea-odoctype-labelp" title={record.odoctypespan}>{record.odoctypespan}</p>);
      //     }
      //   }
      // } else {
      column,
      // }
    );

    return newColumns;
  }


  searchCustomRender(form, condtion, fns) {
    let group = [];
    form.isFormInit && toJS(condtion).map((c, index) => {
      let items = [];
      c.items.map((field) => {
        let col = 2;
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@oye3sx@${field.domkey[0]}`}
            label={`${field.label}`}
            labelCol={{ span: `${field.labelcol}` }}
            ratio1to2
            error={form.getError(field)}
            tipPosition="bottom"
            wrapperCol={{ span: `${field.fieldcol}` }}
          >
            <WeaSwitch
              ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@doiic1@${field.domkey[0]}`}
              fieldConfig={field}
              form={form}
              {...fns}
            />
          </WeaFormItem>),
          col,
          hide: form.isHide(field, (keys, allParams) => {
            if (allParams.officalType == '0') {
              if (field.label == '签报环节' || field.label == '收文环节' || field.label == '发文环节') return true;
            }
            if (allParams.officalType == '1') {
              if (field.label == '签报环节' || field.label == '收文环节') return true;
            }
            if (allParams.officalType == '2') {
              if (field.label == '签报环节' || field.label == '发文环节') return true;
            }
            if (allParams.officalType == '3') {
              if (field.label == '收文环节' || field.label == '发文环节') return true;
            }
            return false;
          }),
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@h919j5@${index}`} needTigger={false} title={c.title} showGroup={c.defaultshow} items={items} />);
    });
    return group;
  }
}
